package com.example.hrsystem.workflow.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;

import com.example.hrsystem.R;
import com.example.hrsystem.workflow.WorkflowViewModel;
import com.example.hrsystem.workflow.model.ProcessInstance;
import com.example.hrsystem.workflow.model.WorkflowTask;
import com.google.android.material.textfield.TextInputEditText;

import java.text.SimpleDateFormat;
import java.util.Locale;

/**
 * 用户任务处理界面Activity
 * 用于处理分配给当前用户的工作流任务
 */
public class UserTaskHandlerActivity extends AppCompatActivity {

    private WorkflowViewModel workflowViewModel;
    private WorkflowTask currentTask;
    
    private TextView taskNameTextView;
    private TextView taskDescriptionTextView;
    private TextView taskStatusTextView;
    private TextView assigneeTextView;
    private TextView createTimeTextView;
    private TextInputEditText commentEditText;
    private Button rejectButton;
    private Button completeButton;
    
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_task_handler);
        
        // 获取传递的任务数据
        currentTask = (WorkflowTask) getIntent().getSerializableExtra("task");
        if (currentTask == null) {
            Toast.makeText(this, "任务数据无效", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }
        
        // 初始化ViewModel
        workflowViewModel = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication())).get(WorkflowViewModel.class);
        
        // 初始化UI组件
        initUI();
        
        // 填充任务数据
        populateTaskData();
        
        // 设置按钮点击事件
        setupClickListeners();
    }
    
    /**
     * 初始化UI组件
     */
    private void initUI() {
        taskNameTextView = findViewById(R.id.text_task_name);
        taskDescriptionTextView = findViewById(R.id.text_task_description);
        taskStatusTextView = findViewById(R.id.text_task_status);
        assigneeTextView = findViewById(R.id.text_assignee);
        createTimeTextView = findViewById(R.id.text_create_time);
        commentEditText = findViewById(R.id.edit_comment);
        rejectButton = findViewById(R.id.button_reject);
        completeButton = findViewById(R.id.button_complete);
    }
    
    /**
     * 填充任务数据到UI
     */
    private void populateTaskData() {
        taskNameTextView.setText(currentTask.getName());
        taskDescriptionTextView.setText(currentTask.getDescription());
        taskStatusTextView.setText("状态: " + currentTask.getStatus());
        assigneeTextView.setText("处理人: " + currentTask.getAssigneeId());
        createTimeTextView.setText("创建时间: " + dateFormat.format(currentTask.getCreateTime()));
    }
    
    /**
     * 设置按钮点击事件
     */
    private void setupClickListeners() {
        rejectButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handleTaskAction("REJECT");
            }
        });
        
        completeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handleTaskAction("COMPLETE");
            }
        });
    }
    
    /**
     * 处理任务操作（完成或拒绝）
     * 
     * @param action 操作类型 (COMPLETE/REJECT)
     */
    private void handleTaskAction(String action) {
        String comment = commentEditText.getText().toString().trim();
        
        // 这里应该调用WorkflowEngine来处理任务
        // 由于我们没有真实的用户系统，这里使用示例用户ID 1
        try {
            workflowViewModel.handleTask(currentTask.getId(), 1, action);
            Toast.makeText(this, "任务已" + ("COMPLETE".equals(action) ? "完成" : "拒绝"), Toast.LENGTH_SHORT).show();
            // 返回上一界面
            finish();
        } catch (Exception e) {
            Toast.makeText(this, "处理任务失败: " + e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }
}